package View.MeanShiftDialog;

import View.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MeanShiftDialog extends JDialog implements ActionListener {
    MainFrame parentFrame;
    JTextField jtfRd = new JTextField();
    JTextField jtfRc = new JTextField();
    JTextField jtfIter = new JTextField();
    JTextField jtfMaxLevels = new JTextField();
    JButton jbtnOK = new JButton("OK");
    JButton jbtnCancel = new JButton("Cancel");

    boolean closeFlag;
    private int iterations;
    private int maxLevel;
    private int rd;
    private int rc;

    static final String OK_COMMAND = "OK_COMMAND";
    static final String CANCEL_COMMAND = "CANCEL_COMMAND";



    public MeanShiftDialog(MainFrame parentFrame) {
        this.parentFrame = parentFrame;
        setLocationRelativeTo(parentFrame);
        setSize(250, 280);
        setResizable(false);
        setTitle("Mean shift parameters");
        setModal(true);
        setVisible(false);



        jbtnOK.addActionListener(this);
        jbtnOK.setActionCommand(OK_COMMAND);

        jbtnCancel.addActionListener(this);
        jbtnCancel.setActionCommand(CANCEL_COMMAND);

        GridBagLayout gb = new GridBagLayout();
        setLayout(gb);
        
        JLabel jlRadD = new JLabel("Set distance rad: ");
        JLabel jlRadC = new JLabel("Set color rad: ");
        JLabel jlLevel = new JLabel("Set number of level: ");
        JLabel jlIter = new JLabel("Set number of iterations: ");

        gb.setConstraints(jlRadD, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jlRadD);

        gb.setConstraints(jtfRd, new GridBagConstraints(0, 2, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfRd);

        gb.setConstraints(jlRadC, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jlRadC);

        gb.setConstraints(jtfRc, new GridBagConstraints(0, 4, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfRc);

        gb.setConstraints(jlLevel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jlLevel);

        gb.setConstraints(jtfMaxLevels, new GridBagConstraints(0, 6, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfMaxLevels);

        gb.setConstraints(jlIter, new GridBagConstraints(0, 7, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jlIter);

        gb.setConstraints(jtfIter, new GridBagConstraints(0, 8, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 10, 0), 0, 0));
        add(jtfIter);

        gb.setConstraints(jbtnOK, new GridBagConstraints(0, 9, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnOK);
        gb.setConstraints(jbtnCancel, new GridBagConstraints(2, 9, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 10), 0, 0));
        add(jbtnCancel);

    }

    public boolean runDialog() {
        closeFlag = false;
        setVisible(true);
        return  closeFlag;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals(OK_COMMAND)) {
            boolean errFlag = false;
            try
            {
                iterations = Integer.valueOf(jtfIter.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            try
            {
                rd = Integer.valueOf(jtfRd.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            try
            {
                rc = Integer.valueOf(jtfRc.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            try
            {
                maxLevel = Integer.valueOf(jtfMaxLevels.getText());
            }
            catch (NumberFormatException nfe)
            {
                errFlag = true;
            }
            if (errFlag) {
                JOptionPane.showMessageDialog(this, "Error parameters.", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (iterations <= 0) {
                JOptionPane.showMessageDialog(this, "Iterations number must be positive", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (maxLevel < 0) {
                JOptionPane.showMessageDialog(this, "Number of levels can't be negative", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            if (rd <= 0 || rc <= 0) {
                JOptionPane.showMessageDialog(this, "Radius must be positive", "Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }
            closeFlag = true;
            setVisible(false);

        }
        if (e.getActionCommand().equals(CANCEL_COMMAND)) {
            setVisible(false);
        }


    }

    public int getIterations() {
        return iterations;
    }

    public int getRd() {
        return rd;
    }

    public int getRc() {
        return rc;
    }

    public int getMaxLevel() {
        return maxLevel;
    }
}